﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using BankCharts.Silverlight.ViewModel.SubViewModel;
using GalaSoft.MvvmLight.Command;

namespace BankCharts.Silverlight.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            PrepareCommand();
        }

        #region ApplicationState
        private ApplicationState _ApplicationState = ApplicationState.IdleState;

        /// <summary>
        /// Gets the ApplicationState property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ApplicationState ApplicationState
        {
            get
            {
                return _ApplicationState;
            }

            set
            {
                if (_ApplicationState == value)
                {
                    return;
                }

                _ApplicationState = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("ApplicationState");
            }
        }
        #endregion

        #region InfoString
        private string _InfoString = string.Empty;

        /// <summary>
        /// Gets the InfoString property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string InfoString
        {
            get
            {
                return _InfoString;
            }

            set
            {
                _InfoString = value;

                if (string.IsNullOrWhiteSpace(value))
                {
                    ApplicationState = ApplicationState.IdleState;
                }
                else
                {
                    ApplicationState = ApplicationState.InfoPrintState;
                }

                // Update bindings, no broadcast
                RaisePropertyChanged("InfoString");
            }
        }
        #endregion

        #region UnparsedBankDataManager
        private UnparsedBankDataManagerVM _UnparsedBankDataManager = new UnparsedBankDataManagerVM();

        /// <summary>
        /// Gets the UnparsedBankDataManager property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public UnparsedBankDataManagerVM UnparsedBankDataManager
        {
            get
            {
                return _UnparsedBankDataManager;
            }

            set
            {
                if (_UnparsedBankDataManager == value)
                {
                    return;
                }

                _UnparsedBankDataManager = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("UnparsedBankDataManager");
            }
        }
        #endregion

        #region CategoryManager
        private CategoryManagerVM _CategoryManager = new CategoryManagerVM();

        /// <summary>
        /// Gets the CategoryManager property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public CategoryManagerVM CategoryManager
        {
            get
            {
                return _CategoryManager;
            }

            set
            {
                if (_CategoryManager == value)
                {
                    return;
                }

                _CategoryManager = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("CategoryManager");
            }
        }
        #endregion

        #region StatisticsManager
        private StatisticsManagerVM _StatisticsManager = new StatisticsManagerVM();

        /// <summary>
        /// Gets the StatisticsManager property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public StatisticsManagerVM StatisticsManager
        {
            get
            {
                return _StatisticsManager;
            }

            set
            {
                if (_StatisticsManager == value)
                {
                    return;
                }

                _StatisticsManager = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("StatisticsManager");
            }
        }
        #endregion

        #region Commands

        public RelayCommand<SelectionChangedEventArgs> TabSelectionChangedCommand { get; set; }
        public RelayCommand CloseInfoCommand { get; set; }

        public void PrepareCommand()
        {
            TabSelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>(TabSelectionChanged);
            CloseInfoCommand = new RelayCommand(CloseInfo);
        }

        public void CloseInfo()
        {
            InfoString = string.Empty;
        }

        public void TabSelectionChanged(SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                TabItem item = e.AddedItems[0] as TabItem;
                TabControl parent = item.Parent as TabControl;

                if (parent.SelectedIndex == 2)
                {
                    int nbTotalLines;
                    int nbValidLines;
                    StatisticsManager.PrepareStatistics(CategoryManager.Categories, UnparsedBankDataManager.Initialize(out nbTotalLines, out nbValidLines));
                    InfoString = nbValidLines + " lines parsed and valid over " + nbTotalLines + " lines.";
                }
            }
        }

        #endregion
    }
}
